home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Random2.0 / Source / DieRoller.m < prev    next >
Text File  |  1995-06-12  |  1KB  |  73 lines

  1. //
  2. // DieRoller
  3. //
  4. // Copyright (C) 1992 Contemporary Design Studios. All rights reserved.
  5. //
  6.  
  7.  
  8. #import "DieRoller.h"
  9.  
  10.  
  11. @implementation DieRoller
  12.  
  13.  
  14. //
  15. //rollDie:
  16. //
  17.  
  18. - (int)rollDie:(int) numSides
  19. {
  20.     return [self randMax:(numSides - 1)] + 1;
  21. }
  22.  
  23.  
  24. //
  25. // roll:die:
  26. //
  27.  
  28. - (int)roll:(int) numRolls die:(int) numSides
  29. {
  30.     int temp = 0;
  31.     int loop;
  32.     
  33.     for (loop = 1 ; loop <= numRolls ; loop++ )
  34.     temp += [self rollDie:numSides];
  35.     
  36.     return temp;
  37. }
  38.  
  39.  
  40. //
  41. // rollBest:of:die:
  42. //
  43.  
  44. - (int) rollBest:(int)numWanted of:(int)numRolls die:(int)numSides
  45. {
  46.     int temp[numRolls];                // Array of rolls
  47.     int loop1;                    // First loop control variable
  48.     int loop2;                    // Second loop control variable
  49.     int highest;                // Index of highest found roll
  50.     int accumulator = 0;            // Accumulates total best roll
  51.     
  52.     for (loop1 = 1 ; loop1 <= numRolls ; loop1++)    // Fill an array with rolls
  53.     temp[loop1] = [self rollDie:numSides];
  54.     
  55.     for (loop1 = 1 ; loop1 <= numWanted; loop1++) {
  56.     highest = 1;                // Start off as if first is highest
  57.     for (loop2 = 2 ; loop2 <= numRolls ; loop2++)    // Scan array for higher rolls
  58.         if (temp[loop2] > temp[highest])    // If temp[loop2] is higher, then
  59.         highest = loop2;        //     remember that fact
  60.     accumulator += temp[highest];        // Add highest roll to accumulator
  61.     temp[highest] = 0;            // Clear highest roll so we don't find it again
  62.     }
  63.     
  64.     return accumulator;                // Return what we found
  65. }
  66.  
  67.  
  68. @end
  69.  
  70.  
  71. //
  72. // End of file.
  73. //